1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    *   - Redistributions of source code must retain the above copyright
9    *     notice, this list of conditions and the following disclaimer.
10   *
11   *   - Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in the
13   *     documentation and/or other materials provided with the distribution.
14   *
15   *   - Neither the name of Oracle nor the names of its
16   *     contributors may be used to endorse or promote products derived
17   *     from this software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  
33  /**
34   * I love blinking things.
35   *
36   * @author Arthur van Hoff
37   * @author 04/24/96 Jim Hagen use getBackground
38   * @author 02/05/98 Mike McCloskey removed use of deprecated methods
39   * @author 04/23/99 Josh Bloch, use timer instead of explicit multithreading.
40   * @author 07/10/00 Daniel Peek brought to code conventions, minor changes
41   */
42  import java.awt.Color;
43  import java.awt.Dimension;
44  import java.awt.Font;
45  import java.awt.FontMetrics;
46  import java.awt.Graphics;
47  import java.util.StringTokenizer;
48  import java.util.Timer;
49  import java.util.TimerTask;
50  
51  
52  public class Blink extends java.applet.Applet {
53  
54      private static final long serialVersionUID = -775844794477507646L;
55      private Timer timer;              // Schedules the blinking
56      private String labelString;       // The label for the window
57      private int delay;                // the delay time between blinks
58  
59      @Override
60      public void init() {
61          String blinkFrequency = getParameter("speed");
62          delay = (blinkFrequency == null) ? 400
63                  : (1000 / Integer.parseInt(blinkFrequency));
64          labelString = getParameter("lbl");
65          if (labelString == null) {
66              labelString = "Blink";
67          }
68          Font font = new java.awt.Font("Serif", Font.PLAIN, 24);
69          setFont(font);
70      }
71  
72      @Override
73      public void start() {
74          timer = new Timer();     //creates a new timer to schedule the blinking
75          timer.schedule(new TimerTask() {      //creates a timertask to schedule
76  
77              // overrides the run method to provide functionality
78              @Override
79              public void run() {
80                  repaint();
81              }
82          }, delay, delay);
83      }
84  
85      @Override
86      public void paint(Graphics g) {
87          int fontSize = g.getFont().getSize();
88          int x = 0, y = fontSize, space;
89          int red = (int) (50 * Math.random());
90          int green = (int) (50 * Math.random());
91          int blue = (int) (256 * Math.random());
92          Dimension d = getSize();
93          g.setColor(Color.black);
94          FontMetrics fm = g.getFontMetrics();
95          space = fm.stringWidth(" ");
96          for (StringTokenizer t = new StringTokenizer(labelString);
97                  t.hasMoreTokens();) {
98              String word = t.nextToken();
99              int w = fm.stringWidth(word) + space;
100             if (x + w > d.width) {
101                 x = 0;
102                 y += fontSize;  //move word to next line if it doesn't fit
103             }
104             if (Math.random() < 0.5) {
105                 g.setColor(new java.awt.Color((red + y * 30) % 256,
106                         (green + x / 3) % 256, blue));
107             } else {
108                 g.setColor(getBackground());
109             }
110             g.drawString(word, x, y);
111             x += w;  //shift to the right to draw the next word
112         }
113     }
114 
115     @Override
116     public void stop() {
117         timer.cancel();  //stops the timer
118     }
119 
120     @Override
121     public String getAppletInfo() {
122         return "Title: Blinker\n"
123                 + "Author: Arthur van Hoff\n"
124                 + "Displays multicolored blinking text.";
125     }
126 
127     @Override
128     public String[][] getParameterInfo() {
129         String pinfo[][] = {
130             { "speed", "string", "The blink frequency" },
131             { "lbl", "string", "The text to blink." }, };
132         return pinfo;
133     }
134 }